package com.example.stars_project_android.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.stars_project_android.R;
import com.example.stars_project_android.entity.BlackUserEntity;
import com.example.stars_project_android.entity.SelfMessage;

import java.util.Date;
import java.util.List;

public class MessageAdapter extends BaseAdapter {
    private Context mContext;
    private int layoutResId;
    private List<SelfMessage> messagesList;

    public MessageAdapter(Context mContext, int layoutResId, List<SelfMessage> messagesList) {
        this.mContext = mContext;
        this.layoutResId = layoutResId;
        this.messagesList = messagesList;
    }

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (messagesList != null){
            return messagesList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MessageAdapter.ViewHolder viewHolder;
        if (convertView == null){
            //加载item的布局文件，生成一个View对象，再给converView赋值
            convertView = LayoutInflater.from(mContext).inflate(layoutResId,null);
            //获取每一个item中的控件对象
            viewHolder = new ViewHolder();

            viewHolder.friendId = convertView.findViewById(R.id.userId);
            viewHolder.date = convertView.findViewById(R.id.post_time);
            viewHolder.message = convertView.findViewById(R.id.information);
            //将ViewHolder对象同converView关联起来
            convertView.setTag(viewHolder);
        }else {//重复利用convertView对象
            //获取到跟当前的convertView绑定在一起的ViewHolder对象
            viewHolder = (MessageAdapter.ViewHolder) convertView.getTag();
        }
        //给控件对象绑定要显示的数据

        viewHolder.friendId.setText(messagesList.get(position).getFirendId());
        viewHolder.date.setText(messagesList.get(position).getDate().toString());
        viewHolder.message.setText(messagesList.get(position).getMessage());
        return convertView;
    }

    private static class ViewHolder{
        //每一个item中的控件
        TextView friendId;
        TextView message;
        TextView date;
    }
}
